<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <!--<el-form-item label="巡检任务" prop="taskName">-->
        <!--<el-input-->
          <!--v-model="queryParams.taskName"-->
          <!--placeholder="请输入巡检任务"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="巡检计划" prop="scheduleId">
        <el-select v-model="queryParams.scheduleId" placeholder="请选择计划" @change="selectForm(form.scheduleId)">
          <el-option v-for="item in schedules" :key="item.id" :label="item.scheduleName" :value="item.id" />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="任务执行时间" prop="execTime">-->
        <!--<el-date-picker clearable size="small"-->
          <!--v-model="queryParams.execTime"-->
          <!--type="date"-->
          <!--value-format="yyyy-MM-dd"-->
          <!--placeholder="选择任务执行时间">-->
        <!--</el-date-picker>-->
      <!--</el-form-item>-->
      <el-form-item label="责任人" prop="responsible">
        <el-input
          v-model="queryParams.responsible"
          placeholder="请输入责任人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="任务状态" prop="state">-->
        <!--<el-input-->
          <!--v-model="queryParams.state"-->
          <!--placeholder="请输入任务状态"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="任务结果" prop="result">
        <el-input
          v-model="queryParams.result"
          placeholder="请输入任务结果"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="字段3" prop="responsible">-->
        <!--<el-input-->
          <!--v-model="queryParams.responsible"-->
          <!--placeholder="请输入字段3"-->
          <!--clearable-->
          <!--size="small"-->
          <!--@keyup.enter.native="handleQuery"-->
        <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="模块类型" prop="classifyType">-->
        <!--<el-select v-model="queryParams.classifyType" placeholder="请选择模块类型" clearable size="small">-->
          <!--<el-option label="请选择字典生成" value="" />-->
        <!--</el-select>-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['security:scheduleTask:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['security:scheduleTask:edit']"
        >修改任务</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['security:scheduleTask:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['security:scheduleTask:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="scheduleTaskList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="序号" align="center" prop="id" />-->
      <el-table-column label="巡检任务" align="center" prop="taskName" />
      <!--<el-table-column label="计划id" align="center" prop="scheduleId" />-->
      <el-table-column label="所属计划" align="center" prop="scheduleName" />
      <el-table-column label="任务生成时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预计执行时间" align="center" prop="expectedTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expectedTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行时间" align="center" prop="execTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.execTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="责任人" align="center" prop="responsible" />
      <!--<el-table-column label="任务状态" align="center" prop="state" />-->
      <el-table-column label="任务结果" align="center" prop="result" />
      <el-table-column label="备注" align="center" prop="remark" />
      <!--<el-table-column label="字段3" align="center" prop="responsible" />-->
      <!--<el-table-column label="模块类型" align="center" prop="classifyType" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['security:scheduleTask:edit']"
          >修改任务</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdateData(scope.row)"
            v-hasPermi="['activiti:actMaintainOrderData:edit']"
          >修改数据</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['security:scheduleTask:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!--表单配置详情-->
    <el-dialog title="表单数据" :visible.sync="detailOpen" width="50%" append-to-body>
      <div class="test-form">
        <parser :key="new Date().getTime()" :form-conf="detailForm" @submit="submitDetailForm" />
      </div>
    </el-dialog>

    <!-- 添加或修改巡检任务对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="巡检任务" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入巡检任务" />
        </el-form-item>
        <el-form-item label="计划模板" prop="scheduleId">
          <el-select v-model="form.scheduleId" placeholder="请选择计划" @change="selectForm(form.scheduleId)">
            <el-option v-for="item in schedules" :key="item.id" :label="item.scheduleName" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="预计执行时间" prop="expectedTime">
          <el-date-picker clearable size="small"
                          v-model="form.expectedTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择任务执行时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="任务执行时间" prop="execTime">
          <el-date-picker clearable size="small"
            v-model="form.execTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择任务执行时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="责任人" prop="responsible">
          <el-input v-model="form.responsible" placeholder="请输入责任人" />
        </el-form-item>
        <!--<el-form-item label="任务状态" prop="state">-->
          <!--<el-input v-model="form.state" placeholder="请输入任务状态" />-->
        <!--</el-form-item>-->
        <el-form-item label="任务结果" prop="result">
          <el-input v-model="form.result" placeholder="请输入任务结果" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <!--<el-form-item label="字段3" prop="responsible">-->
          <!--<el-input v-model="form.responsible" placeholder="请输入字段3" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="模块类型" prop="classifyType">-->
          <!--<el-select v-model="form.classifyType" placeholder="请选择模块类型">-->
            <!--<el-option label="请选择字典生成" value="" />-->
          <!--</el-select>-->
        <!--</el-form-item>-->
        <!--<el-divider content-position="center">巡检表单信息</el-divider>-->
        <!--<div class="test-form">-->
          <!--<parser :key="new Date().getTime()" :form-conf="detailForm"/>-->
        <!--</div>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listScheduleTask, getScheduleTask, delScheduleTask, addScheduleTask, updateScheduleTask } from "@/api/smartParks/security/scheduleTask";
import Parser from '@/components/parser/Parser';
import { getActMaintainOrderDataVO,delActMaintainOrderData, addActMaintainOrderData,
  updateActMaintainOrderData,updateActMaintainOrderDataVO } from "@/api/system/activiti/actMaintainOrderData";
import { getActMaintainOrder,decodeFields,createProcessInstance} from "@/api/system/activiti/actMaintainOrder";
import { getMaintainOrder, listMaintainOrder} from "@/api/smartParks/security/maintainOrder";

export default {
  name: "ScheduleTask",
  components: {
    Parser
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 巡检任务表格数据
      scheduleTaskList: [],
      detailOpen: false,
      detailForm: {
        fields: []
      },
      schedules:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        taskName: null,
        scheduleId: null,
        execTime: null,
        expectedTime:null,
        formDataId: null,
        state: null,
        result: null,
        responsible: null,
        classifyType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // 计划id 必填
        scheduleId: [
          { required: true, message: "计划id不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    //this.queryParams
    listMaintainOrder().then(response => {
      this.schedules = response.rows;
    });
  },
  methods: {
    handleUpdateData(row){
      //this.reset();
      //this.form.id=row.id;
      //formId,预览
      //获取 form desc
      getActMaintainOrderDataVO(row.formData).then(response => {
        // 设置值
        const data = response.data;
        this.detailForm = {
          ...JSON.parse(data.formConf),
          fields: decodeFields(data.formFields),
          formId:row.formId,
          id:row.id
        }
        // 设置表单的值
        this.detailForm.fields.forEach(item => {
          const val = data.formVariables[item.__vModel__]
          if (val) {
            item.__config__.defaultValue = val
          }
        });
        // 弹窗打开
        this.detailOpen = true
      });
    },
    selectForm(scheduleId){
      //scheduleId
      let obj = this.schedules.find((item)=>{
        return item.id === scheduleId;
      });
      this.form.formId = obj.formId;
        // getActMaintainOrder(obj.formId).then(response => {
        //   // 设置值
        //   const data = response.data;
        //   this.detailForm = {
        //     ...JSON.parse(data.conf),
        //     disabled: true, // 表单禁用
        //     formBtns: false, // 按钮隐藏
        //     fields: decodeFields(data.fields),
        //   }
        // })
    },
    submitDetailForm(params){
      if (!params) {
        return;
      }
      // 设置表单禁用
      const conf = params.conf;
      conf.disabled = true; // 表单禁用
      conf.formBtns = false; // 按钮隐藏
      // 提交表单，创建流程
      const variables = params.values;
      createProcessInstance({
        id:params.id,
        formId: params.formId,
        formVariables: variables
      }).then(response => {
        this.msgSuccess("发起流程成功");
        // 关闭当前窗口
        // this.$tab.closeOpenPage();
        // this.$router.go(-1);
        this.detailOpen = false;
      }).catch(() => {
        conf.disabled = false; // 表单开启
        conf.formBtns = true; // 按钮展示
      })
    },
    /** 查询巡检任务列表 */
    getList() {
      this.loading = true;
      listScheduleTask(this.queryParams).then(response => {
        this.scheduleTaskList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        taskName: null,
        scheduleId: null,
        execTime: null,
        expectedTime:null,
        formData: null,
        formId: null,
        state: null,
        result: null,
        remark: null,
        responsible: null,
        classifyType: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加巡检任务";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getScheduleTask(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改巡检任务";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {

          if (this.form.id != null) {
            updateScheduleTask(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addScheduleTask(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除巡检任务编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delScheduleTask(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('security/scheduleTask/export', {
        ...this.queryParams
      }, `security_scheduleTask.xlsx`)
    }
  }
};
</script>
